import React, { useState, useEffect } from 'react';
import axios from 'axios';

const WorkSummaryForm = ({ applicationId }) => {
    const [workSummary, setWorkSummary] = useState({
        content: ''
    });

    useEffect(() => {
        const fetchWorkSummary = async () => {
            try {
                const response = await axios.get(`/api/applications/${applicationId}/work-summary`);
                setWorkSummary(response.data);
            } catch (error) {
                console.error('Error fetching work summary:', error);
            }
        };

        if (applicationId) {
            fetchWorkSummary();
        }
    }, [applicationId]);

    const handleChange = (e) => {
        const { name, value } = e.target;
        setWorkSummary(prevInfo => ({
            ...prevInfo,
            [name]: value
        }));
    };

    const handleSubmit = async (e) => {
        e.preventDefault();
        try {
            await axios.post(`/api/applications/${applicationId}/work-summary`, workSummary);
            alert('工作技术总结保存成功！');
        } catch (error) {
            console.error('Error saving work summary:', error);
            alert('保存失败，请重试。');
        }
    };

    return (
        <form onSubmit={handleSubmit} className="form-container">
            <h3>工作技术总结</h3>
            <div className="form-group">
                <label>内容:</label>
                <textarea name="content" value={workSummary.content} onChange={handleChange} rows="10" required></textarea>
            </div>
            <button type="submit">保存</button>
        </form>
    );
};

export default WorkSummaryForm;

